﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using AimGameLibrary;
using AimGameLibrary.GameObject;

namespace AimGameEngine.EditorObject
{
   

    public class NodeGroup : Image2D
    {
        public const String Group = "Group";

        List<IDraw> ListDraw;
        Dictionary<String, NodeGroup> MyList;
        Texture2D[] GroupTexture;
        List<ViewData> GroupViewData;
        List<NodeObject> NodeSelect;

        Vector2 MinimizePosition;
        List<Vector2> OriginalPosition;
        int Edge = 5;
        Rectangle Minimize;
        
        //float amounts = 0.2f;

        StateGroup MyState;
        public StateGroup MyStatE { get { return MyState; } }

        public Rectangle MinimizE { get { return Minimize; } }
        public Rectangle BodyminimizE { get { return new Rectangle((int)this.camera.ApplyPosition(this).X, (int)this.camera.ApplyPosition(this).Y, Minimize.Width, Minimize.Height); } }

        Rectangle Maximize;
        public Rectangle MaximizE { get { return Maximize; } }
        public Rectangle BodymaximizE { get { return new Rectangle((int)this.camera.ApplyPosition(this).X, (int)this.camera.ApplyPosition(this).Y, Maximize.Width, Maximize.Height); } }

        public Rectangle ReSize { get { return new Rectangle((int)(this.camera.ApplyPosition(this).X + GroupViewData[2].Position.X), (int)(this.camera.ApplyPosition(this).Y + GroupViewData[2].Position.Y), GroupViewData[2].Source.Width, GroupViewData[2].Source.Height); } }
        public Rectangle BodyCancle { get { return new Rectangle((int)(this.camera.ApplyPosition(this).X + GroupViewData[3].Position.X), (int)(this.camera.ApplyPosition(this).Y + GroupViewData[3].Position.Y), GroupViewData[3].Source.Width, GroupViewData[3].Source.Height); } }

        public NodeGroup(SpriteBatch inputspritebatch, Camera inputcamera, List<IDraw> ListDraw, Dictionary<String, NodeGroup> MyList)
            : base(inputspritebatch, inputcamera)
        {
            GroupTexture = new Texture2D[4];
            GroupViewData = new List<ViewData>();
            GroupViewData.Add(new ViewData());
            GroupViewData.Add(new ViewData());
            GroupViewData.Add(new ViewData());
            GroupViewData.Add(new ViewData());

            NodeSelect = new List<NodeObject>();
            OriginalPosition = new List<Vector2>();
            this.ListDraw = ListDraw;
            this.MyList = MyList;

            MinimizePosition = new Vector2(5, 28 + 10);
            Minimize = new Rectangle(0, 0, 180, 120);
            this.MyViewData.Depth = 1f;
            MyViewData.Colors = EditorColor.Default.GroupNode_Purple;

            MyState = StateGroup.Maximize;
        }

        public void LoadTexture(Texture2D Title, Texture2D Body, Texture2D Control)
        {
            GroupTexture[0] = Title;
            GroupTexture[1] = Body;
            GroupTexture[2] = Control;
            GroupTexture[3] = Control;

            GroupViewData[0].Source = new Rectangle(0, 0, GroupTexture[0].Width, GroupTexture[0].Height);
            GroupViewData[1].Position = new Vector2(0, GroupTexture[0].Height);
            GroupViewData[1].Source = new Rectangle(0, 0, GroupTexture[0].Width, GroupTexture[1].Height);

            GroupViewData[2].Position = new Vector2(13, 10);
            GroupViewData[2].Source = new Rectangle((GroupTexture[2].Width / 3) * 1, 0, GroupTexture[2].Width / 3, GroupTexture[2].Height);

            GroupViewData[3].Source = new Rectangle((GroupTexture[2].Width / 3) * 2 , 0, GroupTexture[2].Width / 3, GroupTexture[2].Height);

            for (int i = 0; i < GroupViewData.Count; i++)
            {
                GroupViewData[i].Origin = Vector2.Zero;
                GroupViewData[i].Rotation = 0f;
                GroupViewData[i].Scale = Vector2.One;
            }
        }

        public void Setup(List<NodeObject> NodeSelect)
        {
            this.NodeSelect.Clear();
            this.OriginalPosition.Clear();

            foreach (NodeObject node in NodeSelect)
                this.NodeSelect.Add(node);

            if (this.NodeSelect.Count > 0)
            {
                CalculateSize();

                SetupBody();

                foreach (NodeObject nodeRound2 in this.NodeSelect)
                    OriginalPosition.Add(nodeRound2.MyViewData.Position - MyViewData.Position);
            }
        }

        void CalculateSize()
        { 
            List<float> x = new List<float>();
            List<float> y = new List<float>();
            foreach (NodeObject node in NodeSelect)
            {
                x.Add(node.MyViewData.Position.X);
                y.Add(node.MyViewData.Position.Y);
            }


            MyViewData.Position = new Vector2(x.Min() - Edge, y.Min() - GroupTexture[0].Height - Edge);
            Maximize = new Rectangle(0, 0, (int)(x.Max() 
                + (Edge * 2) 
                + NodeSelect[0].MyViewData.Source.Width 
                - MyViewData.Position.X)
                                         , (int)(y.Max() 
                + NodeSelect[0].MyViewData.Source.Height 
                + GroupTexture[0].Height 
                - MyViewData.Position.Y));
            
            SetupSize(Maximize);
        }
        
        public void SetupState()
        {
            if (MyStatE == StateGroup.Minimize)
            {
                MyState = StateGroup.Maximize;
                SetupSize(Maximize);
                GroupViewData[2].Source = new Rectangle((GroupTexture[2].Width / 3) * 1, 0, GroupTexture[2].Width / 3, GroupTexture[2].Height);
            }
            else if (MyStatE == StateGroup.Maximize)
            {
                MyState = StateGroup.Minimize;
                SetupSize(Minimize);
                GroupViewData[2].Source = new Rectangle((GroupTexture[2].Width / 3) * 0, 0, GroupTexture[2].Width / 3, GroupTexture[2].Height);
            }

            SetupBody();
            Move();
        }

        void SetupSize(Rectangle input)
        {
            MyViewData.Source = input;
        }

        void SetupBody()
        {

            #region Title
            GroupViewData[0].Scale = new Vector2((float)MyViewData.Source.Width / (float)GroupTexture[0].Width, 1);
            #endregion

            #region Body
            //if (MyViewData.Source.Height > GroupTexture[1].Height)
            //    Vector2.Lerp(GroupViewData[1].Scale, new Vector2((float)MyViewData.Source.Width / (float)GroupTexture[1].Width, ((float)MyViewData.Source.Height - (float)GroupTexture[0].Height) / (float)GroupTexture[1].Height),amounts);
            //else Vector2.Lerp(GroupViewData[1].Scale, new Vector2(MyViewData.Source.Width / GroupTexture[1].Width, 1),amounts);
            if (MyViewData.Source.Height > GroupTexture[1].Height)
                GroupViewData[1].Scale = new Vector2((float)MyViewData.Source.Width / (float)GroupTexture[1].Width, ((float)MyViewData.Source.Height - (float)GroupTexture[0].Height) / (float)GroupTexture[1].Height);
            else GroupViewData[1].Scale = new Vector2(MyViewData.Source.Width / GroupTexture[1].Width, 1);
            #endregion

            #region Cancle
            GroupViewData[3].Position = new Vector2(((float)MyViewData.Source.Width / (float)GroupTexture[0].Width) - 20, 10);
            #endregion

        }

        public void AddMetoList()
        {
            ListDraw.Add(this);
            MyList.Add(MyViewData.Name, this);

            this.MyViewData.Depth = 1f - (float)MyList.Count/100;
        }

        public void Remove()
        {
            ListDraw.Remove(this);
            MyList.Remove(MyViewData.Name);
        }

        public void Move()
        {
            for (int i = 0; i < NodeSelect.Count; i++)
                if (MyStatE == StateGroup.Maximize)
                    NodeSelect[i].MyViewData.Position = MyViewData.Position + OriginalPosition[i];
                else if (MyStatE == StateGroup.Minimize)
                    NodeSelect[i].MyViewData.Position = MyViewData.Position + MinimizePosition;
        }

        protected override void SubDraw()
        {
            if (GroupTexture[0] != null && GroupTexture[1] != null && GroupTexture[2] != null)
            {
                for (int i = 0; i < GroupViewData.Count; i++)
                {
                    spritebatch.Draw(this.GroupTexture[i]
                                        , this.camera.ApplyPosition(this) + GroupViewData[i].Position
                                        , GroupViewData[i].Source
                                        , MyViewData.Colors
                                        , MyViewData.Rotation
                                        , MyViewData.Origin
                                        , GroupViewData[i].Scale
                                        , MyViewData.Spriteeffects
                                        , MyViewData.Depth - (float)(0.001f * i));
                }
            }


        }

        public void Save(ArrayList list)
        {
            list.Add(MyStatE);
            list.Add(NodeSelect.Count);

            #region Pack List
            String TempPacketName = String.Empty;

            for (int i = 0; i < NodeSelect.Count; i++)
            {
                if (i > 0)
                    TempPacketName += "|";
                //TempPacketName += DictionaryObjectUpdateIsdown[key0].MyData.MyName;
                TempPacketName += NodeSelect[i].MyData.MyName;
            }
            #endregion

            list.Add(TempPacketName);
        }
    }
}
